package com.xiongkw;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class TestListNode {
    private int val;
    private TestListNode next;

    TestListNode(int x) {
        val = x;
    }

    //两个大数相加
    public static TestListNode addList(TestListNode n1,TestListNode n2){
        if(n1 == null || n2 == null){
            return n1 == null ? n2 : n1;
        }
        TestListNode result = new TestListNode(0);
        TestListNode pointer = result;
        TestListNode pointerPrev = null;
        TestListNode n1Pointer = n1;
        TestListNode n2Pointer = n2;
        boolean  overFlow = false;
        while (n1Pointer != null && n2Pointer != null){
            overFlow = false;
            pointerPrev  = pointer;
            pointer.setVal(pointer.getVal()+ n1Pointer.getVal() + n2Pointer.getVal());

            n1Pointer = n1Pointer.getNext();
            n2Pointer = n2Pointer.getNext();

            if(pointer.getVal() >= 10){
                overFlow = true;
                pointer.setNext(new TestListNode(pointer.getVal()/10));
                pointer.setVal(pointer.getVal() % 10);
                pointer = pointer.getNext();
            }else if (n1Pointer != null && n2Pointer != null){
                pointer.setNext(new TestListNode(0));
                pointer = pointer.getNext();
                pointerPrev = pointer;
            }
        }
        TestListNode restPointer = n1Pointer == null ? n2Pointer : n1Pointer;
        if(!overFlow){
            pointer.setNext(restPointer);
        }else {
            pointerPrev.setNext(addList(pointer,restPointer));
        }
        return result;
    }

    //反转list
    public static TestListNode reverseList(TestListNode head){
        if(head == null || head.getNext() == null){
            return head;
        }
        TestListNode prev = null,cur = head,next = null;
        while(cur != null){
            next = cur.getNext();
            cur.setNext(prev);
            prev = cur;
            cur = next;
        }
        return prev;
    }

    //打印list
    public static void printList(TestListNode head){
        if(head == null){
            System.out.println("null list");
        }
        String seperator = "";
        StringBuilder sb = new StringBuilder();
        sb.append(head.getVal()).append(seperator);
        TestListNode next = head.getNext();
        while (next != null){
            sb.append(next.getVal()).append(seperator);
            next = next.getNext();
        }
        System.out.println(sb.substring(0,sb.length()-seperator.length()));
    }

    public static TestListNode buildNumberToList(String ss){
        if(StringUtils.isBlank(ss)){
            return null;
        }
        char[] chars = ss.toCharArray();
        TestListNode result = new TestListNode(0);
        TestListNode pointer = result;
        for(int i = chars.length; i>0; i--){
            int n = Integer.valueOf(String.valueOf(chars[i-1]));
            if(n > 10){
                throw new IllegalArgumentException();
            }
            pointer.setNext(new TestListNode(n));
            pointer = pointer.getNext();
        }
        return result.getNext();
    }

    public static void main(String[] args) {

        TestListNode n1 = TestListNode.buildNumberToList("12334444");
        TestListNode n2 = TestListNode.buildNumberToList("12334444");

        TestListNode result = TestListNode.addList(n1, n2);
        TestListNode.printList(TestListNode.reverseList(result));
    }
}
